package com.jookes.signplugin.main;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.block.SignChangeEvent;
import org.bukkit.event.player.PlayerInteractEvent;

/**
 * This class represents the SignHandler. It listens for specific events.
 * @author Jens Walrave
 */
public class SignListener implements Listener {

	private Main plugin;
	
	public SignListener(Main plugin) {
		this.plugin = plugin;
		
	}

	/**
	 * This EventHandler is called when a player interacts with a block. It checks if the block is a sign.
	 * Depending on the command written on the sign, an action will be executed.
	 */
	@EventHandler
	public void onPlayerInteract(PlayerInteractEvent event) {
		if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
			
			Block block = event.getClickedBlock();
			Player player = event.getPlayer();
			
			// Check if the block is a sign
			if (block.getType().equals(Material.SIGN) || block.getType().equals(Material.SIGN_POST) || block.getType().equals(Material.WALL_SIGN)) {
				Sign sign = (Sign) block.getState();
				String[] lines = sign.getLines();
				
				if(lines[0].contains("[heal]")) {
					player.sendMessage("This is a healing sign");
				}else if(lines[0].contains("[buy]")){
					plugin.getShopHandler().buyItem(player, Integer.parseInt(lines[1]), lines[2], Double.parseDouble(lines[3]));
				}else if(lines[0].contains("[sell]")){
					plugin.getShopHandler().sellItem(player, Integer.parseInt(lines[1]), lines[2], Double.parseDouble(lines[3]));
				}
			}
		}
	}
	
	@EventHandler
	public void onSignChange(SignChangeEvent event) {
		//String[] lines = event.getLines();
		//int i = 0;
		//for(String line : lines) {
			//event.setLine(i, ChatColor.DARK_BLUE + line.toLowerCase());
			//i++;
		//}
	}
}